/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.ball.views.obstacles
{
	import com.puny.ball.utils.CollisionPL;
	import com.puny.ball.utils.HalfSideCollisionPL;
	import com.puny.ball.utils.Vector2D;
	
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	public class HalfSideLineObstacle extends Obstacle
	{
		private var _cpl:CollisionPL = new HalfSideCollisionPL();
		private const _dir:Vector2D = new Vector2D();
		private const _topLeft:Vector2D = new Vector2D();
		private const _bottomLeft:Vector2D = new Vector2D();
		private const _topRight:Vector2D = new Vector2D();;
		private const _bottomRight:Vector2D = new Vector2D();
		public function HalfSideLineObstacle()
		{
			super();
		}
		
		public function inflate(r:Number):void {
			var rect:Rectangle;
			rect = getBounds(this);
			var rsx:Number = r / scaleX;
			var rsy:Number = r / scaleY;
			rect.x -= rsx;
			rect.y -= rsy;
			rect.width += 2 * rsx;
			rect.height += 2 * rsy;
			
			_topLeft.x = calcX(rect.left, rect.top);
			_topLeft.y = calcY(rect.left, rect.top);
			_bottomLeft.x = calcX(rect.left, rect.bottom);
			_bottomLeft.y = calcY(rect.left, rect.bottom);
			_topRight.x = calcX(rect.right, rect.top);
			_topRight.y = calcY(rect.right, rect.top);
			_bottomRight.x = calcX(rect.right, rect.bottom);
			_bottomRight.y = calcY(rect.right, rect.bottom);
			
			_cpl.r.x = (_topLeft.x + _bottomLeft.x) / 2;
			_cpl.r.y = (_topLeft.y + _bottomLeft.y) / 2;
			_dir.x = (_topRight.x + _bottomRight.x) / 2 - _cpl.r.x;
			_dir.y = (_topRight.y + _bottomRight.y) / 2 - _cpl.r.y;
			_cpl.dir = _dir;
//			trace("line:",_topLeft,_bottomLeft,_topRight,_bottomRight,_dir);
//			trace("line:",_cpl.r,_cpl.dir);
			drawDebugShape();
		}
		
		override protected function drawShape():void {
			shape.graphics.clear();
			shape.graphics.lineStyle(2,0xFF0000,.5);
			shape.graphics.moveTo(_cpl.r.x, _cpl.r.y);
			shape.graphics.lineTo(_cpl.dir.x+_cpl.r.x, _cpl.dir.y+_cpl.r.y);
//			trace("Line:",_cpl.r,_cpl.dir);
		}

		private function calcX(px:Number,py:Number):Number {
			var matrix:Matrix = transform.matrix;
			return matrix.a * px - matrix.b * py + matrix.tx;
		}
		
		private function calcY(px:Number, py:Number):Number {
			var matrix:Matrix = transform.matrix;
			return -matrix.c * px + matrix.d * py + matrix.ty;
		}
		
		override public function collision(org:Vector2D, dir:Vector2D):Boolean {
			pos = Infinity;
			var t:Number;
			if (_cpl.setParticle(org,dir)) {
				t = _cpl.collisionPoint();
				if (t < pos) {
					pos = t;
					newPos = _cpl.reflex();
					mirroredPos = _cpl.mirror();
				}
			}
			if (pos != Infinity) {
				hitPos.x = org.x + pos * dir.x;
				hitPos.y = org.y + pos * dir.y;
//				hitPos = _pos;
			}
			return pos != Infinity;
		}
	}
}